﻿#include "GroupDialog.h"
#include "../../MirGraphics/MLibrary.h"
#include "../../../Shared/Globals.h"
#include "../../MirSounds/SoundManager.h"
#include "../../../Shared/Language.h"
#include "../../MirObjects/MapObject.h"
#include "../GameScene.h"
#include "../../../Shared/Enums.h"
#include "../../../Shared/ClientPackets.h"
#include "../../MirNetwork/Network.h"
#include "../../MirControls/MirInputBox.h"

using namespace Client::MirControls;
using namespace Client::MirGraphics;
using namespace Client::MirNetwork;
using namespace Client::MirObjects;
using namespace Client::MirSounds;
namespace C = ClientPackets;

namespace Client::MirScenes::Dialogs
{

bool GroupDialog::AllowGroup = false;
std::vector<std::string> GroupDialog::GroupList;

    GroupDialog::GroupDialog()
    {
        setIndex(120);
        setLibrary(Libraries::Prguse);
        setMovable(true);
        setSort(true);
        setLocation(getCenter());

        GroupMembers = std::vector<MirLabel*>(Globals::MaxGroup);

        MirLabel *tempVar = new MirLabel();
        tempVar->setAutoSize(true);
        Point tempVar2(16, 33);
        tempVar->setLocation(&tempVar2);
        tempVar->setParent(this);
        tempVar->setNotControl(true);
        GroupMembers[0] = tempVar;

        for (int i = 1; i < GroupMembers.size(); i++)
        {
            MirLabel *tempVar3 = new MirLabel();
            tempVar3->setAutoSize(true);
            Point tempVar4(((i + 1) % 2) * 100 + 16, 55 + ((i - 1) / 2) * 20);
            tempVar3->setLocation(&tempVar4);
            tempVar3->setParent(this);
            tempVar3->setNotControl(true);
            GroupMembers[i] = tempVar3;

            delete tempVar3;
        }



        TitleLabel = new MirImageControl();
        TitleLabel->setIndex(5);
        TitleLabel->setLibrary(Libraries::Title);
        Point tempVar5(18, 8);
        TitleLabel->setLocation(&tempVar5);
        TitleLabel->setParent(this);

        CloseButton = new MirButton();
        CloseButton->setHoverIndex(361);
        CloseButton->setIndex(360);
        Point tempVar6(206, 3);
        CloseButton->setLocation(&tempVar6);
        CloseButton->setLibrary(Libraries::Prguse2);
        CloseButton->setParent(this);
        CloseButton->setPressedIndex(362);
        CloseButton->setSound(SoundList::ButtonA);
        CloseButton->Click += [&] (o, e)
        {
            Hide();
        };

        SwitchButton = new MirButton();
        SwitchButton->setHoverIndex(115);
        SwitchButton->setIndex(114);
        Point tempVar7(25, 219);
        SwitchButton->setLocation(&tempVar7);
        SwitchButton->setLibrary(Libraries::Prguse);
        SwitchButton->setParent(this);
        SwitchButton->setPressedIndex(116);
        SwitchButton->setSound(SoundList::ButtonA);
        SwitchButton->setHint(GameLanguage::GroupSwitch);
        SwitchButton->Click += [&] (o, e)
        {
            C::SwitchGroup *tempVar8 = new C::SwitchGroup();
            tempVar8->AllowGroup = !AllowGroup;
            Network::Enqueue(tempVar8);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar8' statement was not added since tempVar8 was passed to a method or constructor. Handle memory management manually.
        };

        AddButton = new MirButton();
        AddButton->setHoverIndex(134);
        AddButton->setIndex(133);
        Point tempVar9(70, 219);
        AddButton->setLocation(&tempVar9);
        AddButton->setLibrary(Libraries::Title);
        AddButton->setParent(this);
        AddButton->setPressedIndex(135);
        AddButton->setSound(SoundList::ButtonA);
        AddButton->setHint(GameLanguage::GroupAdd);
        AddButton->Click += [&] (o, e)
        {
            AddMember();
        };

        DelButton = new MirButton();
        DelButton->setHoverIndex(137);
        DelButton->setIndex(136);
        Point tempVar10(140, 219);
        DelButton->setLocation(&tempVar10);
        DelButton->setLibrary(Libraries::Title);
        DelButton->setParent(this);
        DelButton->setPressedIndex(138);
        DelButton->setSound(SoundList::ButtonA);
        DelButton->setHint(GameLanguage::GroupRemove);
        DelButton->Click += [&] (o, e)
        {
            DelMember();
        };

        BeforeDraw += GroupPanel_BeforeDraw;

        GroupList.clear();

        delete tempVar;
    }

    void GroupDialog::GroupPanel_BeforeDraw(std::any sender, EventArgs *e)
    {
        if (GroupList.empty())
        {
            AddButton->setIndex(130);
            AddButton->setHoverIndex(131);
            AddButton->setPressedIndex(132);
        }
        else
        {
            AddButton->setIndex(133);
            AddButton->setHoverIndex(134);
            AddButton->setPressedIndex(135);
        }
        if (GroupList.size() > 0 && GroupList[0] != MapObject::User->Name)
        {
            AddButton->setVisible(false);
            DelButton->setVisible(false);
        }
        else
        {
            AddButton->setVisible(true);
            DelButton->setVisible(true);
        }

        if (AllowGroup)
        {
            SwitchButton->setIndex(117);
            SwitchButton->setHoverIndex(118);
            SwitchButton->setPressedIndex(119);
        }
        else
        {
            SwitchButton->setIndex(114);
            SwitchButton->setHoverIndex(115);
            SwitchButton->setPressedIndex(116);
        }

        for (int i = 0; i < GroupMembers.size(); i++)
        {
            GroupMembers[i]->setText(i >= GroupList.size() ? "" : GroupList[i]);
        }
    }

    void GroupDialog::AddMember(const std::string &name)
    {
        if (GroupList.size() >= Globals::MaxGroup)
        {
            GameScene::Scene->ChatDialog->ReceiveChat("Your group already has the maximum number of members.", ChatType::System);
            return;
        }
        if (GroupList.size() > 0 && GroupList[0] != MapObject::User->Name)
        {
            GameScene::Scene->ChatDialog->ReceiveChat("You are not the leader of your group.", ChatType::System);
            return;
        }

        C::AddMember *tempVar = new C::AddMember();
        tempVar->Name = name;
        Network::Enqueue(tempVar);

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
    }

    void GroupDialog::AddMember()
    {
        if (GroupList.size() >= Globals::MaxGroup)
        {
            GameScene::Scene->ChatDialog->ReceiveChat("Your group already has the maximum number of members.", ChatType::System);
            return;
        }
        if (GroupList.size() > 0 && GroupList[0] != MapObject::User->Name)
        {

            GameScene::Scene->ChatDialog->ReceiveChat("You are not the leader of your group.", ChatType::System);
            return;
        }

        MirInputBox *inputBox = new MirInputBox(GameLanguage::GroupAddEnterName);

        inputBox->OKButton->Click += [&] (o, e)
        {
            C::AddMember *tempVar = new C::AddMember();
            tempVar->Name = inputBox->InputTextBox->getText();
            Network::Enqueue(tempVar);
            inputBox->Dispose();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        };
        inputBox->Show();

        delete inputBox;
    }

    void GroupDialog::DelMember()
    {
        if (GroupList.size() > 0 && GroupList[0] != MapObject::User->Name)
        {

            GameScene::Scene->ChatDialog->ReceiveChat("You are not the leader of your group.", ChatType::System);
            return;
        }

        MirInputBox *inputBox = new MirInputBox(GameLanguage::GroupRemoveEnterName);

        inputBox->OKButton->Click += [&] (o, e)
        {
            C::DelMember *tempVar = new C::DelMember();
            tempVar->Name = inputBox->InputTextBox->getText();
            Network::Enqueue(tempVar);
            inputBox->Dispose();

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
        };
        inputBox->Show();

        delete inputBox;
    }
}
